<template>
  <div class="xtx-city">
    <!-- 默认显示 -->
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="!seled.fullSels">请选择配送地址</span>
      <span class="value" v-else>{{seled.fullSels}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 选择省、市、区弹层 -->
    <div class="option" v-if="active">
      <span class="ellipsis"
      @click="selectCity(city)"
       v-for="city in cityList"
       :key="city.code">
       {{city.name}}
       </span>
    </div>
  </div>
</template>
<script>
import { ref, onMounted, reactive } from 'vue'
import axios from 'axios'

const useCityList = () => {
  const cityList = ref([])
  const cityListBup = []
  onMounted(async () => {
    const { data } = await axios({
      url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
    })
    // console.log('11', data)
    cityListBup.push(...data)
    cityList.value = data
  })
  return { cityList, useCityList, cityListBup }
}

export default {
  name: 'XtxCity',
  emits: ['change'],
  setup (props, { emit }) {
    // 控制选择层是否显示
    const active = ref(false)
    // 点击交互
    // 1. 每一个单项中都有一个名称为areaList的字段存放着当前下面的子级列表数据
    // 2. 每一个单项中都有一个名称为level字段 代表当前是省->0  市->1 区->2
    // 交互功能实现
    // 点击选中城市数据
    const seled = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countryCode: '', // 地区code
      countryName: '', // 地区名
      // 最终选择
      fullSels: ''
    })
    // 数据列表
    const { cityList, cityListBup } = useCityList()
    // 市
    const selectCity = (child) => {
      cityList.value = child.areaList
      if (child.level === 0) {
        seled.provinceCode = child.code
        seled.provinceName = child.name
      } else if (child.level === 1) {
        seled.cityCode = child.code
        seled.cityName = child.name
      } else {
        seled.countryCode = child.code
        seled.countryName = child.name
        seled.fullSels = `${seled.provinceName} ${seled.cityName} ${seled.countryName}`
        // 选完了=》1. 关闭选择框 2. 子传父（选中的数据）
        toggleDialog()
        emit('change', seled)
      }
    }

    // 打开弹层
    const open = () => {
      active.value = true
      cityList.value = cityListBup
    }
    // 关闭弹层
    const close = () => {
      active.value = false
    }
    const toggleDialog = () => {
      // active.value = !active.value
      if (active.value) {
        close()
      } else {
        open()
      }
    }
    return { active, toggleDialog, cityList, selectCity, seled }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
